home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0787.arc / IWPAS.ARC / HEXUTIL.P < prev    next >
Encoding:
Text File  |  1987-04-28  |  2.8 KB  |  115 lines

  1. {-------------------------------------------------------}
  2. { HEXUTIL.INC                                           }
  3. { Hexadecimal conversion routines                       }
  4.  
  5. { Copyright (c) 1987, Ciarcia's Circuit Cellar          }
  6. {    All Rights Reserved                                }
  7.  
  8. TYPE
  9.  
  10.  Hextype    = STRING[4];        { input & output string }
  11.  
  12.  
  13. {-------------------------------------------------------}
  14. { Converts a hex string of some hexits into the         }
  15. {  corresponding integer.                               }
  16. { Returns error code in case the numbers were bad       }
  17.  
  18. PROCEDURE HexToInt(hexits  : Hextype;
  19.                VAR intval  : INTEGER;
  20.                VAR errcode : INTEGER);
  21.  
  22. BEGIN
  23.  
  24.  Val('$'+hexits,intval,errcode);
  25.  
  26. END;
  27.  
  28.  
  29.  
  30. {-------------------------------------------------------}
  31. { convert "unsigned" 16-bit number to REAL 0..64K       }
  32.  
  33. FUNCTION WordToReal(fullword:INTEGER) : REAL;
  34.  
  35. BEGIN
  36.  
  37.   IF fullword < 0
  38.    THEN WordToReal := 65536.0 + fullword
  39.    ELSE WordToReal := fullword;
  40.  
  41. END;
  42.  
  43.  
  44. {-------------------------------------------------------}
  45. { converts one 4-bit nybble to a character              }
  46.  
  47. FUNCTION NybToHex(nybval:INTEGER) : HexType;
  48.  
  49. BEGIN
  50.  CASE nybval OF
  51.   0..9   : NybToHex := Chr($30+nybval);
  52.   10..15 : NybToHex := Chr($41+nybval-10);
  53.  END;  { CASE }
  54. END;
  55.  
  56.  
  57.  
  58. {-------------------------------------------------------}
  59. { converts a 16-bit number to characters                }
  60.  
  61. FUNCTION IntToHex(intval:INTEGER) : Hextype;
  62.  
  63. VAR
  64.  tempstring     : Hextype;
  65.  tempval        : INTEGER;
  66.  
  67. BEGIN
  68.  
  69.  tempstring := '0000';
  70.  
  71. {--- the first nybble depends on the sign               }
  72.  
  73.  IF intval < 0                  { < 0, need trick       }
  74.   THEN BEGIN
  75.    tempval := intval XOR $FFFF;         { 1's compl     }
  76.    tempval := tempval DIV 4096;         { bits to LSD   }
  77.    tempval := tempval XOR $000F;        { flip again    }
  78.    tempstring[1] := NybToHex(tempval);  { now convert   }
  79.   END
  80.   ELSE BEGIN                    { >= 0, simple convert  }
  81.    tempstring[1] := NybToHex(
  82.                       (intval AND $F000) DIV 4096);
  83.   END;
  84.  
  85. {--- the rest are easy                                  }
  86.  
  87.  tempstring[2] := NybToHex((intval AND $0F00) DIV 256);
  88.  tempstring[3] := NybToHex((intval AND $00F0) DIV 16);
  89.  tempstring[4] := NybToHex( intval AND $000F);
  90.  
  91.  IntToHex := tempstring;
  92.  
  93. END;
  94.  
  95.  
  96. {-------------------------------------------------------}
  97. { converts byte to characters                           }
  98.  
  99. FUNCTION ByteToHex(intval:INTEGER) : Hextype;
  100.  
  101. VAR
  102.  tempstring     : Hextype;
  103.  
  104. BEGIN
  105.  
  106.  tempstring := '00';
  107.  
  108.  tempstring[1] := NybToHex((intval AND $00F0) DIV 16);
  109.  tempstring[2] := NybToHex( intval AND $000F);
  110.  
  111.  ByteToHex := tempstring;
  112.  
  113. END;
  114.  
  115.